package com.soul.quartz.ssm.controller;

import com.alibaba.fastjson.JSONObject;
import com.soul.quartz.ssm.service.QuartzService;
import org.quartz.JobKey;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/quartz")
public class QuartzController {

    @Autowired
    private QuartzService quartzService;

    @RequestMapping(value="/list")
    public String list(Model model) throws SchedulerException {
        List list = quartzService.jobList();
        model.addAttribute("list",list);
        return "list";
    }

    @PostMapping("/add")
    public String add(String cron,String clazz,String jobName,
                      String jobGroup,String triggerName,String triggerGroup) throws ClassNotFoundException, SchedulerException {

        if (clazz != null){
            clazz = clazz.substring(0,1).toUpperCase() + clazz.substring(1);
            clazz = clazz.endsWith("Job") ? clazz : clazz + "Job";
            if (!clazz.contains("\\.")){
                clazz = "com.soul.quartz.ssm.job."+clazz;
            }
        }

        Class cls = Class.forName(clazz);
        quartzService.add(cron, cls, jobName, jobGroup, triggerName, triggerGroup);
        return "redirect:/quartz/list?msg=success";
    }

    @RequestMapping(value="/toAdd")
    public String toAdd(Model model) {
        return "add";
    }

    @RequestMapping(value="/toEdit")
    public String toEdit(String jobName,String jobGroup,Model model) throws SchedulerException {
        JobKey jobKey = new JobKey(jobName,jobGroup);
        Map<String,String> map = quartzService.getJob(jobKey);
        model.addAttribute("job",map);
        return "edit";
    }

    @PostMapping("/edit")
    public String edit(String oldJobName, String oldJobGroup,String oldTriggerName,String oldTriggerGroup,
                        String cron,String clazz,String jobName, String jobGroup,String triggerName,
                       String triggerGroup,Model model) throws ClassNotFoundException, SchedulerException {

        if (clazz != null){
            clazz = clazz.substring(0,1).toUpperCase() + clazz.substring(1);
            clazz = clazz.endsWith("Job") ? clazz : clazz + "Job";
            if (!clazz.contains("\\.")){
                clazz = "com.soul.quartz.ssm.job."+clazz;
            }
        }
        Class cls = Class.forName(clazz);
        boolean result = quartzService.updateJob(oldJobName, oldJobGroup, oldTriggerName, oldTriggerGroup,
                jobName, jobGroup, triggerName, triggerGroup, cron, cls);
        String msg;
        if(result){
            msg = "修改任务成功!";
        }else{
            msg = "修改任务失败!";
        }
        return "redirect:/quartz/list?msg="+msg;
    }


    @PostMapping("/pause")
    @ResponseBody
    public String pause(@RequestParam("jobName") String jobName, @RequestParam("jobGroup") String jobGroup) throws SchedulerException {
        JSONObject json = new JSONObject();

        if(StringUtils.isEmpty(jobName) || StringUtils.isEmpty(jobGroup)){
            json.put("code", 500);
        }else{
            quartzService.pause(jobName, jobGroup);
            json.put("code", 200);
        }

        return json.toJSONString();
    }

    @PostMapping("/resume")
    @ResponseBody
    public String resume(@RequestParam("jobName") String jobName,@RequestParam("jobGroup") String jobGroup) throws SchedulerException {
        JSONObject json = new JSONObject();

        if(StringUtils.isEmpty(jobName) || StringUtils.isEmpty(jobGroup)){
            json.put("code", 500);
        }else{
            quartzService.resume(jobName, jobGroup);
            json.put("code", 200);
        }

        return json.toJSONString();
    }

    @PostMapping("/delete")
    @ResponseBody
    public String delete(@RequestParam("jobName") String jobName,@RequestParam("jobGroup") String jobGroup,
                            @RequestParam("triggerName") String triggerName,@RequestParam("triggerGroup") String triggerGroup ) throws SchedulerException {
        JSONObject json = new JSONObject();

        if(StringUtils.isEmpty(jobName) || StringUtils.isEmpty(jobGroup) ||
                StringUtils.isEmpty(triggerName) || StringUtils.isEmpty(triggerGroup) ){
            json.put("code", 500);
        }else{
            quartzService.delete(jobName, jobGroup, triggerName, triggerGroup);
            json.put("code", 200);
        }

        return json.toJSONString();
    }

}
